'use strict';

const db = uniCloud.database();
const collection = db.collection('family');
exports.main = async (event, context) => {
	const {
		familyId,
		username
	} = event;
	const res = await collection.where({ familyId }).get();
	// 如果还没有这个家庭，则为其生成一个新的家庭
	if (res.data.length === 0) {
		const res = await collection.add({
			familyId,
			members: [username],
			name: username + '的家庭',
			cateList: [{
				categoryId: 'cat123',
				categoryName: '感冒药'
			}]
		});
		return {
			code: 0,
			data: res.data
		};
	}

	//返回数据给客户端
	return {
		code: 0,
		data: res.data
	};
};